<?php

namespace App\Console\Commands\model;


use App\Dep\JingdongTagCategoryDep;
use App\Dep\JingdongTagDep;
use App\Dep\ModelDep;
use App\Dep\SystemDep;
use App\Dep\TrainLogDep;
use App\Enum\CommonEnum;
use App\Enum\TrainEnum;
use App\Lib\AigcSdk;
use App\Lib\DouYinWebSdk;
use App\Lib\JdSdk;
use App\Lib\JdWebSdk;
use App\Lib\SdSdk;
use App\Lib\Taobao\domain\ResourceCheckRequestDto;
use App\Lib\Taobao\request\GuangguangContentBrandSearchRequest;
use App\Lib\Taobao\Top;
use App\Lib\XiaoHongSdk;
use App\Service\UidService;
use Illuminate\Console\Command;
use App\Lib\Jos\JosClient;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Illuminate\Support\Facades\Date;
use TopClient;

class CheckTagCmd extends Command
{

    //打标
    public $signature = 'model:check-tag';

    public function handle()
    {
        $dep = new ModelDep();
        $logDep = new TrainLogDep();
        $systemDep = new SystemDep();

        $resTagIng = $dep->getTagIng();
        if($resTagIng->isEmpty()){
            $this->log('no tag ing model');
            return false;
        }

        $resSystem = $systemDep->first();

        $resTagIng->each(function($item) use ($systemDep,$dep,$logDep,$resSystem){
            $tagDir = "{$resSystem->tag_dir}\\{$item->tag_batch}";
            $sourceArr = json_decode($item->source,true);
            foreach ($sourceArr as &$source){
                $tagPath = $tagDir . '\\' . $source['title'] . '.txt' ;
                $sourcePath = $tagDir . '\\' . $source['source'];
                if(file_exists($tagPath)){
                    $this->log('update tag',['id' => $item['id'],'path' => $tagPath]);
                    $source['origin_tag'] = file_get_contents($tagPath);

//                    $trainDir = "{$resSystem->train_dir}\\{$item->name}";
//                    if(!file_exists($trainDir)){
//                        mkdir($trainDir);
//                    }
//
//                    $trainSourcePath = "{$trainDir}\\{$source['source']}";
//                    $trainTagPath = "{$trainDir}\\{$source['title']}.txt";
//
//                    copy($sourcePath,$trainSourcePath);
//                    copy($tagPath,$trainTagPath);
//
//                    $this->log('copy to train',['sourcePath'=> $sourcePath,'trainSourcePath' => $trainSourcePath,'tagPath' => $tagPath,'trainTagPath'=> $trainTagPath]);
                }
            }
            $data = [
              'source' => json_encode($sourceArr)
            ];
            if($this->checkOk($sourceArr)){
                $this->log('update ok',['id' => $item['id']]);

                $data['status'] = TrainEnum::TRAIN_STATUS_TAG_OK;
                $data['tag_ok_at'] = date('Y-m-d H:i:s');
                $data['is_tag'] = CommonEnum::NO;

                $logData = [
                  'content' => '打标完成',
                  'model_id' => $item['id'],
                ];
                $logDep->add($logData);
            }
            $this->log('update source',['id' => $item['id']]);
            $dep->edit($item->id,$data);
        });
    }

    private function checkOk($sourceArr){
        foreach ($sourceArr as $source){
            if(empty($source['origin_tag'])){
                return false;
            }
        }
        return true;
    }

    private function log($msg, $context = [])
    {
        $this->info("sync log: {$msg} " . json_encode($context));
        log_daily("check-tag")->info($msg, $context);
    }


}
